# -*- coding: utf-8 -*-
# @Author : zhaochen
# @Date   : 2019/4/17
# @File   : Rule28_Ind_UnpaidCreditClass5Sate_Check.py
# @E-mail : zhaochen@bbdservice.com

'''eof
name:申请人未结清信贷五级分类校验
code:Ind_UnpaidCreditClass5Sate_Check_2D
tableName:
columnName:
groups:准入>个人征信校验
dependencies:IC_DSJ_2D
type:常用指标
datasourceType:在线指标
description:
eof'''

import sys

reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def getTable(data, tableName, occurTimes):
    '''
        获取表信息
        :param data:表上层的字典对象
        :param tableName:表的名字
        :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
        :return:该表全部数据
        '''
    table = data.get(tableName)
    if occurTimes == 0:
        if table is None:
            return [{}]
        elif table == "":
            return ""
        elif isinstance(table, list) is False or len(table) == 0:
            return None
        else:
            return data.get(tableName)

    if occurTimes == 1:
        if table == "":
            return ""
        if table is None or isinstance(table, list) is False or len(table) == 0:
            return None
        elif len(table) == 0:
            return [{}]
        else:
            return data.get(tableName)

def isfloat(x):
    '''
    判断x为float类型且大于等于0
    '''
    try:
        if isinstance(float(x), float) and float(x) >= 0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False

def formatData(interface):
    import json
    try:
        data = json.loads(interface[0]["icr"])
        return data if isinstance(data, dict) else {}
    except:
        return {}
#借贷账户信息  PDA  [1..1]  借贷账户信息单元  PD01  [0..*]
# 基本信息段  PD01A  [1..1]  #账户类型  PD01AD01  [1..1]
# 最新表现信息段  PD01B  [1..1]
# 余额  PD01BJ01 [1..1] 五级分类  PD01BD03  [0..1]
###
# 最近一次月度表现信息段  PD01C  [0..1]
#余额  PD01CJ01  [1..1]
#五级分类  PD01CD02  [0..1]

def check():
    data = IC_DSJ_2D['data']
    PDA = getTable(data, 'PDA', occurTimes=1)
    if PDA is None:
        return u'缺失值'
    elif PDA == [{}] or PDA == "":
        return 0
    else:
        PD01 = getTable(PDA[0], 'PD01', occurTimes=0)
        if PD01 is None or PD01 == "":
            return None
        elif PD01 == [{}]:
            return 0
        else:
            result = []
            for i in PD01:
                try:
                    # 基本信息段
                    PD01A = getTable(i, 'PD01A', 1)
                    # 最新表现信息段
                    PD01B = getTable(i, 'PD01B', 1)
                    # 最近一次月度表现信息段
                    PD01C = getTable(i, 'PD01C', 0)
                    if PD01A in null_type_list or (PD01B in null_type_list and PD01C in null_type_list):
                        result.append(None)
                    else:
                        # 账户类型 必须有
                        accountType = PD01A[0].get('PD01AD01')
                        # 余额 必须有
                        PD01BJ01 = PD01B[0].get('PD01BJ01')
                        # 最新表现信息表的 五级分类 可能有
                        PD01BD03 = PD01B[0].get('PD01BD03')
                        # class5state = getTable(PD01B[0], 'PD01BD03', 0)
                        # 余额 必须有
                        PD01CJ01 = PD01C[0].get('PD01CJ01')
                        # 最近一次月度表现信息段 五级分类 可能有
                        PD01CD02 = PD01C[0].get('PD01CD02')
                        count = 0
                        if accountType in ['D1', 'R4', 'R1']:
                            if PD01BD03 in null_type_list or not isfloat(PD01BJ01):
                                result.append(None)
                            else:
                                if PD01BD03 in ['2', '3', '4', '5']:
                                    if isfloat(PD01BJ01):
                                        if float(PD01BJ01) > 0:
                                            count += 1
                                            result.append(1)
                                        else:
                                            result.append(0)
                                    else:
                                        result.append(None)
                                else:
                                    result.append(0)

                            if PD01CD02 in null_type_list or not isfloat(PD01CJ01):
                                result.append(None)
                            else:
                                if count:
                                    continue
                                if PD01CD02 in ['2', '3', '4', '5']:
                                    if isfloat(PD01CJ01):
                                        if float(PD01CJ01) > 0:
                                            result.append(1)
                                        else:
                                            result.append(0)
                                    else:
                                        result.append(None)
                                else:
                                    result.append(0)
                        else:
                            result.append(0)
                except:
                    result.append(None)
            result = filter(lambda x: x is not None, result)
            if len(result) == 0:
                return None
            else:
                return sum(result)


def Ind_UnpaidCreditClass5Sate_Check():
    result = check()
    if result is None:
        return u'缺失值'
    else:
        return result


result = Ind_UnpaidCreditClass5Sate_Check()



